<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="设备编码" prop="bookCode">
        <el-input v-model="queryParams.bookCode" placeholder="请输入设备编码" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="bookName">
        <el-input v-model="queryParams.bookName" placeholder="请输入设备名称" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备类型" prop="typeId">
        <treeselect v-model="queryParams.typeId" :options="equipmentTypeOption" :normalizer="normalizerazSblx"
          style="width: 240px" placeholder="请选择设备类型" />
      </el-form-item>
      <el-form-item label="安装区域" prop="shopId">
        <treeselect v-model="queryParams.shopId" :options="anzhuangqyOption" :normalizer="normalizerazqy"
          style="width: 240px" placeholder="请选择安装区域" />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['equipment:bill:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['equipment:bill:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipment:bill:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shebeiList" row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="设备编码" align="center" prop="bookCode" />
      <el-table-column label="设备名称" align="center" prop="bookName" />
      <el-table-column label="设备类型" align="center" prop="typeName" />
      <el-table-column label="安装区域" align="center" width="150" prop="shopId" :formatter="anzhuangqyFormat" />
      <el-table-column label="维护部门" width="150" align="center" prop="standDeptId" :formatter="whDeptFormat" />
      <el-table-column label="使用部门" width="150" align="center" prop="makeDeptId" :formatter="syDeptFormat" />
      <el-table-column label="证书编号" align="center" prop="parameter" />
      <el-table-column label="设备型号" align="center" prop="bookModel" />
      <el-table-column label="校准周期" width="150" align="center" prop="totalPower" />
      <el-table-column label="出厂日期" width="150" align="center" prop="productionDate" />
      <el-table-column label="校准时间" width="150" align="center" prop="applyDate" />
      <el-table-column label="出厂编号" align="center" prop="productionNumber" />
      <el-table-column label="生产厂家" align="center" prop="specialBook" />

      <el-table-column label="备注" align="center" prop="remarks" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" style="margin-right: 9px"
            @click="handleUpdate(scope.row)" v-hasPermi="['equipment:bill:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:bill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="设备编码" prop="bookCode">
              <el-input v-model="form.bookCode" placeholder="请输入设备编码" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="设备名称" prop="bookName">
              <el-input v-model="form.bookName" placeholder="请输入设备名称" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="设备类型" prop="typeId">
              <treeselect v-model="form.typeId" :options="equipmentTypeOption" :normalizer="normalizerazSblx"
                style="width: 240px" placeholder="请选择设备类型" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="安装区域" prop="shopId">
              <treeselect v-model="form.shopId" :options="anzhuangqyOption" :normalizer="normalizerazqy"
                style="width: 240px" placeholder="请选择安装区域" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="使用部门" prop="makeDeptId">
              <treeselect v-model="form.makeDeptId" :options="deptOption" :normalizer="normalizerazBm"
                style="width: 240px" placeholder="请选择使用部门" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="维护部门" prop="standDeptId">
              <treeselect v-model="form.standDeptId" :options="deptOption" :normalizer="normalizerazBm"
                style="width: 240px" placeholder="请选择使用部门" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="设备型号" prop="bookModel">
              <el-input v-model="form.bookModel" placeholder="请输入设备型号" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="证书编号" prop="parameter">
              <el-input v-model="form.parameter" placeholder="请输入证书编号" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="出厂编号" prop="productionNumber">
              <el-input v-model="form.productionNumber" placeholder="请输入出厂编号" style="width: 240px" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="校准周期(月)" prop="totalPower" label-width="110px">
              <el-input-number v-model="form.totalPower" placeholder="请输入校准周期" controls-position="right" :min="1" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="出厂日期" prop="productionDate">
              <el-date-picker v-model="form.productionDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间" style="width: 240px"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="生产厂家" prop="specialBook">
              <el-input v-model="form.specialBook" placeholder="请输入生产厂家" style="width: 240px" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="校准时间" prop="applyDate">
              <el-date-picker v-model="form.applyDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间" style="width: 240px"></el-date-picker>
            </el-form-item>
          </el-col>
             <el-col :lg="12">
            <el-form-item label="预警天数" prop="alertDays">
              <el-input-number v-model="form.alertDays" controls-position="right" :min="1" placeholder="请输入预警天数" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入备注" style="width: 240px" />
            </el-form-item>
          </el-col>
           
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import { listBill, addBill, updateBill, delBill, detailBill } from "@/api/equipment/bill";
  import { listAllUser } from "@/api/system/user";
  import { getShopByType, treeselect } from "@/api/base/shop";
  import { listEquipmentType } from "@/api/equipment/type";
  import { listDept } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "Bill",
    components: { Treeselect },
    data() {
      return {
        //设备类型下拉
        anzhuangqyOption: [], equipmentTypeOption: [], deptOption: [], UserOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 班组信息表格数据
        teamList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 所属车间字典
        teamWorkshopOptions: [],
        // 禁用状态字典
        statusOptions: [],
        shebeiList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          bookCode: undefined,
          bookName: undefined,
          typeId: undefined,
          shopId: undefined
        },
        // 表单参数
        form: {}, deptOrgOption: [],
        // 表单校验
        rules: {
          bookCode: [{ required: true, message: "设备编码不能为空", trigger: "blur" }],
          bookName: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
          bookModel: [{ required: true, message: "设备型号不能为空", trigger: "blur" }],
          typeId: [{ required: true, message: "设备类型不能为空", trigger: "change" }],
          shopId: [{ required: true, message: "安装区域不能为空", trigger: "change" }],
          makeDeptId: [{ required: true, message: "使用部门不能为空", trigger: "change" }],
          standDeptId: [{ required: true, message: "维护部门不能为空", trigger: "change" }],
          alertDays: [{ required: true, message: "预警天数不能为空", trigger: "blur" }],
          
        },
      };
    },
    created() {
      this.getList();
      /* 安装区域数树 */
      treeselect().then((response) => {
        this.anzhuangqyOption = response.data;
      });
      /* 设备类型树 */
      listEquipmentType().then(res => {
        this.equipmentTypeOption = res.data
      })
      /* 部门树列表 */
      listDept().then(res => {
        this.deptOption = this.handleTree(res.data, "deptId");
        this.deptOrgOption = res.data
      })
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listBill(this.queryParams).then((response) => {
          this.shebeiList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /* 维护部门翻译 */
      whDeptFormat(row) {
        return this.deptOrgOption.filter(item => item.deptId === row.standDeptId)[0]?.deptName
      },
      /* 使用部门翻译 */
      syDeptFormat(row) {
        return this.deptOrgOption.filter(item => item.deptId === row.makeDeptId)[0]?.deptName
      },
      /* 安装区域树转换 */
      getTreeName(list, id) {
        for (let i = 0; i < list.length; i++) {
          if (list[i].id === id) {
            return list[i].label
          } else if (list[i].children && list[i].children.length > 0) {
            let res = this.getTreeName(list[i].children, id)
            if (res) {
              return res
            }
          }
        }
      },
      /* 安装区域翻译 */
      anzhuangqyFormat(row) {
        return this.getTreeName(this.anzhuangqyOption, row.shopId)
      },
      /** 转换安装区域数据结构 */
      normalizerazqy(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.label,
          children: node.children
        };
      },
      /* 设备类型转换结构 */
      normalizerazSblx(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.typeId,
          label: node.typeName,
          children: node.children
        };
      },
      /* 部门转换结构 */
      normalizerazBm(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.bookId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加设备台账";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const bookId = row.bookId || this.ids;
        detailBill(bookId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改设备台账";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.title === '修改设备台账') {
              updateBill(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addBill(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const bookIds = row.bookId || this.ids;
        this.$confirm(
          '是否确认删除设备编码id为"' + bookIds + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delBill(bookIds);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () { });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm("是否确认导出所有设备台账的数据?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return exportTeam(queryParams);
          })
          .then((response) => {
            this.download(response.msg);
          })
          .catch(function () { });
      },
    },
  };
</script>